package com.recorder.myapplication.service;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

/**
 * Created by Administrator on 2019/5/12.
 */


public class ForegroundService extends Service {

    public static final String TAG = "ForegroundService";
    public static final int SERVICE_ID = 1;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.e(TAG, "ForegroundService创建了");
        Log.e(TAG, "当前系统版本是:" + Build.VERSION.SDK_INT);

        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
            //4.3以下
            startForeground(SERVICE_ID, new Notification());
        } else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
            //4.3 -7.0
            startForeground(SERVICE_ID, new Notification());
            startService(new Intent(this, InnerService.class));
        } else {
            Log.e(TAG, "我在这里了333");

            //8.0以上
            NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            NotificationChannel channel = new NotificationChannel("channel", "xx", NotificationManager.IMPORTANCE_LOW);

            if (notificationManager != null) {
                notificationManager.createNotificationChannel(channel);
                Notification notification = new NotificationCompat.Builder(this, "channel").build();

                startForeground(SERVICE_ID, notification);


            }
        }
    }


    public static class InnerService extends Service {

        @Nullable
        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }

        @Override
        public void onCreate() {
            super.onCreate();

            Log.e(TAG, "InnerService创建了");
            startForeground(SERVICE_ID, new Notification());
            stopSelf();

        }

    }
}
